<?php
switch($submenu)
{
	default:
	$where_default = " b.intStatus = 1 AND b.intAktif = 1 ";
	if(!empty($_SESSION["$menu.keyword"]))
		$keyword = $_SESSION["$menu.keyword"];
	elseif(!empty($_POST["keyword"]))
	{
		$keyword = $_POST["keyword"];
		$_SESSION["$menu.keyword"] = $_POST["keyword"];
	}
	if(!empty($keyword))
		$where = " 
		b.vcKode LIKE '%$keyword%' AND $where_default OR 
		b.vcNamaBeli LIKE '%$keyword%' AND $where_default OR
		kat.vcNama LIKE '%$keyword%' AND $where_default OR
		brand.vcNama LIKE '%$keyword%' AND $where_default OR
		grup.vcNama LIKE '%$keyword%' AND $where_default OR
		tipe.vcNama LIKE '%$keyword%' AND $where_default OR
		grade.vcNama LIKE '%$keyword%' AND $where_default OR
		sur.vcNama LIKE '%$keyword%' AND $where_default";
	else
		$where = $where_default;
	$order_default = "NamaBarang";
	if($_GET["ad"])
		$_SESSION["$menu.asc_desc"] = $_GET["ad"];
	elseif(empty($_SESSION["$menu.asc_desc"]))
		$_SESSION["$menu.asc_desc"] = "ASC";
	if($_GET["ob"])
		$_SESSION["$menu.order_by"] = $_GET["ob"];
	elseif(empty($_SESSION["$menu.order_by"]))
		$_SESSION["$menu.order_by"] = $order_default;
	$order_by = $_SESSION["$menu.order_by"]." ".$_SESSION["$menu.asc_desc"];
	$query_barang = "
	SELECT b.intNomor AS NomorBarang, b.vcKode AS KodeBarang, b.vcNamaBeli AS NamaBarang, 
	kat.vcNama AS NamaKategori, b.intNomorMBrand AS NomorBrand, brand.vcNama AS NamaBrand, grup.vcNama AS NamaGroup, 
	tipe.vcNama AS NamaTipe, grade.vcNama AS NamaGrade, sur.vcNama AS NamaSurface
	FROM mbarang b
	JOIN mkategori kat ON b.intNomorMKategori = kat.intNomor JOIN mbrand brand ON b.intNomorMBrand = brand.intNomor
	JOIN mgroup grup ON b.intNomorMGroup = grup.intNomor JOIN mtipe tipe ON b.intNomorMTipe = tipe.intNomor
	JOIN mgrade grade ON b.intNomorMGrade = grade.intNomor JOIN msurface sur ON b.intNomorMSurface = sur.intNomor
	WHERE $where
	ORDER BY $order_by";
//	echo "<br />".$query_barang."<br />;
	$page = new Paging;
	$batas = $_SESSION["setting.row"];
	$posisi = $page->cariPosisi($batas);
	$get_barang = mysql_query("$query_barang LIMIT $posisi, $batas");
	$baris = mysql_num_rows(mysql_query($query_barang));
	$halaman = $page->jumlahHalaman($baris,$batas);
	$link = $page->navHalaman($_GET["p"],$halaman);
	$no = $posisi+1;
?>
	<h2>List Barang</h2>
	<a href="#" class="bt_green" onclick="location.href='?m=barang&md=reload'">
		<span class="bt_green_lft"></span>
		<strong>Reload</strong>
		<span class="bt_green_r"></span>
	</a>
	<table class="rounded-corner">
		<thead>
			<tr>
				<th scope="col" class="rounded-company"></th>
				<?php
				$columns = array("Kode","Nama","Kategori","Brand","Group","Tipe","Grade","Surface");
				$aliases = array("KodeBarang","NamaBarang","NamaKategori","NamaBrand","NamaGroup","NamaTipe","NamaGrade","NamaSurface");
				for($i=0;$i<=7;$i++)
				{
					echo "<th scope=\"col\" class=\"rounded\">";
					$ad = "ASC";
					if($_SESSION["$menu.order_by"] == $aliases[$i])
					{
						$columns[$i] = "<b><u>".$columns[$i]."</u></b>";
						if($_SESSION["$menu.asc_desc"] == "ASC")
							$ad = "DESC";
					}
					echo "<a href=\"?m=$menu&ob=$aliases[$i]&ad=$ad\" style=\"text-decoration:none\">$columns[$i]</a>";
					echo "</th>";
				}
				?>
				<th scope="col" class="rounded"><a>Diskon</a></th>
				<th scope="col" class="rounded-q4"><a>Action</a></th>
			</tr>
		</thead>
		<?php if(!empty($keyword)) { ?>
		<tfoot>
			<tr>
				<td align="right" colspan="10" class="rounded-foot-left"><i>Hasil pencarian: <?php echo $baris; ?></i></td>
				<td class="rounded-foot-right">&nbsp;</td>
			</tr>
		</tfoot>
		<?php } ?>
		<tbody>
<?php
	while($r = mysql_fetch_array($get_barang))
	{
		$get_diskon = mysql_fetch_array(mysql_query("SELECT discount_rate AS DiskonBrand FROM web_discount WHERE customer_id = ".$_SESSION["login.id"]." AND brand_id = ".$r["NomorBrand"]." AND status = 1"));
		if(empty($get_diskon["DiskonBrand"]))
			$get_diskon["DiskonBrand"] = 0;
		echo"
		<tr>
		<td>$no</td>
		<td><a href=\"?m=$menu&md=detail&id=".$r["NomorBarang"]."\">".$r["KodeBarang"]."</a></td>
		<td>".$r["NamaBarang"]."</td>
		<td>".$r["NamaKategori"]."</td>
		<td>".$r["NamaBrand"]."</td>
		<td>".$r["NamaGroup"]."</td>
		<td>".$r["NamaTipe"]."</td>
		<td>".$r["NamaGrade"]."</td>
		<td>".$r["NamaSurface"]."</td>
		<td>".$get_diskon["DiskonBrand"]."%</td>
		<td><a class=\"ask\" href=\"?m=order&md=cart_add&id=".$r["NomorBarang"]."\"><img src=\"../images/cart.png\" width=\"23\"></a></td>
		</tr>";
		$no++;
	}
?>	
		</tbody>
	</table>
<?php
	include "../php/pagination.php";
	break;
	
	case "reload":
	unset($_SESSION["$menu.keyword"]);
	unset($_SESSION["$menu.order_by"]);
	unset($_SESSION["$menu.asc_desc"]);
	die("<meta content='0;URL=?m=barang' http-equiv='refresh' />");
	break;
	
	case "detail":
	$get_detail = mysql_query("SELECT * FROM web_cart_detail WHERE cart_id = ".$_SESSION["cart.id"]." AND good_id = ".$_GET["id"]);
	$check_detail = mysql_num_rows($get_detail);
	if($check_detail > 0)
	{
		$detail = mysql_fetch_array($get_detail);
		$_SESSION["order.warning_box"] = "warning";
		$_SESSION["order.warning_msg"] = "Barang sudah diorder di dalam cart";
		die("<meta content='0;URL=?m=order&md=cart_edit&id=".$detail["id"]."' http-equiv='refresh' />");
	}
	else
	{
		$r = mysql_fetch_array(mysql_query("
		SELECT b.intNomor AS NomorBarang, b.vcKode AS KodeBarang, b.vcNamaBeli AS NamaBarang, 
		kat.vcNama AS NamaKategori, b.intNomorMBrand AS NomorBrand, brand.vcNama AS NamaBrand, grup.vcNama AS NamaGroup, 
		tipe.vcNama AS NamaTipe, grade.vcNama AS NamaGrade, sur.vcNama AS NamaSurface
		FROM mbarang b
		JOIN mkategori kat ON b.intNomorMKategori = kat.intNomor JOIN mbrand brand ON b.intNomorMBrand = brand.intNomor
		JOIN mgroup grup ON b.intNomorMGroup = grup.intNomor JOIN mtipe tipe ON b.intNomorMTipe = tipe.intNomor
		JOIN mgrade grade ON b.intNomorMGrade = grade.intNomor JOIN msurface sur ON b.intNomorMSurface = sur.intNomor
		WHERE b.intNomor = ".$_GET["id"]));
		$get_diskon = mysql_fetch_array(mysql_query("SELECT discount_rate AS DiskonBrand FROM web_discount WHERE customer_id = ".$_SESSION["login.id"]." AND brand_id = ".$r["NomorBrand"]." AND status = 1"));
		if(empty($get_diskon["DiskonBrand"]))
			$get_diskon["DiskonBrand"] = 0;
?>
	<h2>Detail Barang</h2>
	<table><tr><td>
		<div class="form">
			<form action="?m=order&md=cart_add&id=<?php echo $r["NomorBarang"]; ?>" class="niceform" method="post">
				<fieldset>
					<dl>
						<dt><label for="kode">Kode&nbsp;:&nbsp;</label></dt>
						<dd>
							<input name="" size="30" readonly="yes" type="text" value="<?php echo $r["KodeBarang"]; ?>" />
						</dd>
					</dl>
					<dl>
						<dt><label for="nama">Nama&nbsp;:&nbsp;</label></dt>
						<dd><input name="" size="30" readonly="yes" type="text" value="<?php echo $r["NamaBarang"]; ?>" /></dd>
					</dl>
					<dl>
						<dt><label for="kategori">Kategori&nbsp;:&nbsp;</label></dt>
						<dd><input name="" size="30" readonly="yes" type="text" value="<?php echo $r["NamaKategori"]; ?>" /></dd>
					</dl>
					<dl>
						<dt><label for="brand">Brand&nbsp;:&nbsp;</label></dt>
						<dd><input name="" size="30" readonly="yes" type="text" value="<?php echo $r["NamaBrand"]; ?>" /></dd>
					</dl>
					<dl>
						<dt><label for="group">Group&nbsp;:&nbsp;</label></dt>
						<dd><input name="" size="30" readonly="yes" type="text" value="<?php echo $r["NamaGroup"]; ?>" /></dd>
					</dl>
					<dl>
						<dt><label for="tipe">Tipe&nbsp;:&nbsp;</label></dt>
						<dd><input name="" size="30" readonly="yes" type="text" value="<?php echo $r["NamaTipe"]; ?>" /></dd>
					</dl>
					<dl>
						<dt><label for="grade">Grade&nbsp;:&nbsp;</label></dt>
						<dd><input name="" size="30" readonly="yes" type="text" value="<?php echo $r["NamaGrade"]; ?>" /></dd>
					</dl>
					<dl>
						<dt><label for="surface">Surface&nbsp;:&nbsp;</label></dt>
						<dd><input name="" size="30" readonly="yes" type="text" value="<?php echo $r["NamaSurface"]; ?>" /></dd>
					</dl>
					<dl>
						<dt><label for="discount">Diskon&nbsp;:&nbsp;</label></dt>
						<dd><input name="" size="30" readonly="yes" type="text" value="<?php echo $get_diskon["DiskonBrand"]; ?>%" /></dd>
					</dl>
				<dl class="submit"><input id="submit" name="submit" type="submit" value="Tambah ke Cart" /></dl>
				</fieldset>
			</form>
		</div>
	</td></tr></table>
<?php
	}
	break;
}
?>